﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIGameOver : MonoBehaviour
{
    private Text text_CurrentScore;// 本次得分
    //private Text text_BestScore;// 历史最高分
    private Text text_CurrentDiamond;// 本次所得钻石数量
    private Button btn_Rank;// 排行榜
    private Button btn_ReturnMain;// 返回主界面
    private Button btn_Restart;// 再试一次
    private GameObject newImage;

    private void Awake()
    {
        Init();
    }

    /// <summary>
    /// 初始化UIGameOver
    /// </summary>
    private void Init()
    {
        EventCenter.AddListener(CL.EventType.ShowUIGameOver, ShowUIGameOver);
        EventCenter.AddListener(CL.EventType.HideUIGameOver, HideUIGameOver);
        EventCenter.AddListener(CL.EventType.UpdateUIGameOverUI, UpdateUIGameOverUI);
        EventCenter.AddListener(CL.EventType.UpdateNewTrue, UpdateNewTrue);
        EventCenter.AddListener(CL.EventType.UpdateNewFalse, UpdateNewFalse);


        text_CurrentScore = transform.Find("Top/Text_CurrentScore").GetComponent<Text>();
        //text_BestScore = transform.Find("Top/Text_BestScore").GetComponent<Text>();
        text_CurrentDiamond = transform.Find("Top/CurrentDiamond/Text").GetComponent<Text>();
        btn_Rank = transform.Find("Bottom/Btn_Rank").GetComponent<Button>();
        btn_Rank.onClick.AddListener(Btn_Rank);
        btn_ReturnMain = transform.Find("Bottom/Btn_ReturnMain").GetComponent<Button>();        
        btn_ReturnMain.onClick.AddListener(Btn_ReturnMain);
        btn_Restart = transform.Find("Bottom/Btn_Restart").GetComponent<Button>();
        btn_Restart.onClick.AddListener(Btn_Restart);
        btn_Restart.gameObject.transform.Find("Text").GetComponent<Text>().text = "再试一次";
        newImage = transform.Find("Top/New").gameObject;


    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(CL.EventType.ShowUIGameOver, ShowUIGameOver);
        EventCenter.RemoveListener(CL.EventType.HideUIGameOver, HideUIGameOver);
        EventCenter.RemoveListener(CL.EventType.UpdateUIGameOverUI, UpdateUIGameOverUI);
        EventCenter.RemoveListener(CL.EventType.UpdateNewTrue, UpdateNewTrue);
        EventCenter.RemoveListener(CL.EventType.UpdateNewFalse, UpdateNewFalse);
    }

    /// <summary>
    /// 显示、隐藏UIGameOver
    /// </summary>
    private void ShowUIGameOver()
    {
        gameObject.SetActive(true);        
    }
    private void HideUIGameOver()
    {
        gameObject.SetActive(false);
    }

    /// <summary>
    /// 排行榜按钮点击
    /// </summary>
    private void Btn_Rank()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIRankOpen();
    }
    /// <summary>
    /// 返回主界面按钮点击
    /// </summary>
    private void Btn_ReturnMain()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIMainOpen();
    }
    /// <summary>
    /// 再试一次按钮点击
    /// </summary>
    private void Btn_Restart()
    {
        GameManager.Instance.UIGameOpen();
        GameManager.Instance.OnGameStart();
    }

    
    private void UpdateUIGameOverUI()
    {
        text_CurrentScore.text = DataManager.Instance.mGameData.currentScore.ToString();
        //text_BestScore.text = "最高分  " + DataManager.Instance.mUserData.bestScore[0].ToString();
        text_CurrentDiamond.text = "+" + DataManager.Instance.mGameData.currentDiamond.ToString();
    }

    /// <summary>
    /// 显示New
    /// </summary>
    private void UpdateNewTrue()
    {
        if (!newImage.activeSelf)
        {
            newImage.SetActive(true);    
        }         
    }
    /// <summary>
    /// 隐藏New
    /// </summary>
    private void UpdateNewFalse()
    {
        if (newImage.activeSelf)
        {
            newImage.SetActive(false);    
        }         
    }
}
